﻿//
//  GNS_ClientLogin.cpp
//  MobileManage
//
//  Created by mr.l on 16/1/23.
//
//
#include "GnsClientFrame.h"
#include "GNS_ClientHall.h"
#include "GNS_Common.h"
#include "Enum.h"
#include "GNS_LoginScene.h"
#include "GNS_PlayerMgr.h"
#include "GNS_SceneMgr.h"
#include "GNS_HallScene.h" 
#include "CMD_NewGameHall.h"

USING_NS_CC;

extern GNS_HallScene* _hallScene;


GNS_ClientHall::GNS_ClientHall()
{

}

bool GNS_ClientHall::OnHandleSocketMsg(int mainCmd, int subCmd, unsigned char*msgData, int dataLen)
{
	switch (mainCmd)
	{
		case MAIN_MSG_HALL: // 大厅消息
		{
			HandlePacketInitHall(mainCmd,subCmd, msgData, dataLen);
			break;
		}
		case MAIN_MSG_SYSTEM:
		{
			HandlePacketNotice(mainCmd,subCmd, msgData, dataLen);
			break;
		}
		case MAIN_MSG_USER:	  //用户消息
		{
			HandlePacketUser(mainCmd,subCmd,msgData,dataLen);
			break;
		}
		case MAIN_MSG_SHOP:	//商城消息
		{
			HandlePacketShop(mainCmd, subCmd, msgData, dataLen);
			break;
		}
		case  MAIN_MSG_PLAZA_GOLD_RANKING:
		{
			HandlePacketTopList(mainCmd, subCmd, msgData, dataLen);
			break;
		}
		default:
		{
			log("HandlePacketRaw.MainCode[%d].Unknown!", mainCmd);
			break;
		}

     }
    return true;
}

void GNS_ClientHall::connectSuccse()
{
	CMD_Get_HALLGAME_LIST sendInfo;
	sendInfo.iPlatformNo = PLAT_PORM_NO;
	sendData(MAIN_MSG_HALL, SUB_CS_GAME_HALL_LIST, (char*)&sendInfo, sizeof(CMD_Get_HALLGAME_LIST));		    //获取游戏列表信息

	sendData(MAIN_MSG_PLAZA_GOLD_RANKING, SUB_CSG_GOLD_RANKING, 0, 0);	  //获取排行榜

	CMD_InitHall initHall;
	initHall.llUserID = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
	initHall.iGameID = GLOBAL_GAME_ID;

	// 获取设备码info
	string info;
	SystemMgr::getInstance()->getDeviceInfo(info, 1023);
	if (info.length() <= 0)
	{
		//ShowCommonTips("设备码异常");
		return;
	}

	// 计算md5=MD5(info)
	char md5[33] = { 0 };
	memset(&md5, 0, sizeof(md5));
	CCGnsOften::memcpy_safe(md5, 33, SystemMgr::getInstance()->md5String((char*)info.c_str()), strlen(SystemMgr::getInstance()->md5String((char*)info.c_str())));
	// 设备码
	memcpy(initHall.szMachineCode, md5, strlen(md5));
	initHall.szMachineCode[strlen(md5)] = 0;

	sendData(MAIN_MSG_HALL, SUB_CSG_INIT_HALL, (char*)&initHall, sizeof(initHall));
}

void GNS_ClientHall::HandlePacketTopList(int mainCmd, int subCode, unsigned char *pData, int nDataLen)
{
	int iRes = 0;

	GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
	assert(pGnsClientFrame);

	IEventBridge::EVENT_INFO eventInfo;
	eventInfo.iMainCode = mainCmd;
	eventInfo.iSubCode = subCode;
	iRes = eventInfo.copyToBody((char*)pData, nDataLen);
	assert(0 == iRes);
	if (0 == iRes)
	{
		IEventBridge::getInstance()->pushBack(eventInfo);
	}
	switch (subCode)
	{
	case SUB_CSG_GOLD_RANKING: // 金币排行列表
	{
		CMD_SC_GetRanking *pRank = (CMD_SC_GetRanking *)pData;
		if (!pRank || sizeof(CMD_SC_GetRanking) != nDataLen)
		 return;

		map<int, CMD_SC_GetRanking> *pMap = GNS_PlayerMgr::GetInstance()->getRankList();
		pMap->insert(make_pair(pRank->nRankNo, *pRank));

		break;
	}
	default:
	{
			   break;
	}
	}
}

void GNS_ClientHall::HandlePacketNotice(int maiCode,int subCode, unsigned char *pData, int pDataLen)
{

	switch (subCode)
	{
		case SUB_CSG_SYS_MAIL: // 公告
		{
								  
			CMD_SC_SYSMAIL *data = (CMD_SC_SYSMAIL*)pData;
			GNS_PlayerMgr::GetInstance()->setMyNotice(data->szText);
			break;
		}
	}
}

void GNS_ClientHall::HandlePacketShop(int mainCode,int subCode, unsigned char *pData, int nDataLen)
{
		int iRes = 0;

		GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
		assert(pGnsClientFrame);

		IEventBridge::EVENT_INFO eventInfo;
		eventInfo.iMainCode = mainCode;
		eventInfo.iSubCode = subCode;
		iRes = eventInfo.copyToBody((char*)pData, nDataLen);
		assert(0 == iRes);
		if (0 == iRes)
		{
			IEventBridge::getInstance()->pushBack(eventInfo);
		}
}

void GNS_ClientHall::HandlePacketInitHall(int mainCmd, int subCode, unsigned char *pData, int nDataLen)
{
// 	int iRes = 0;
// 
// 	GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
// 	assert(pGnsClientFrame);
// 	IDataRepository* pDataRepository = pGnsClientFrame->_pDataRepository;
// 	assert(pDataRepository);
// 
// 	IEventBridge::EVENT_INFO eventInfo;
// 	eventInfo.iMainCode = mainCmd;
// 	eventInfo.iSubCode = subCode;
// 	iRes = eventInfo.copyToBody((char*)pData, nDataLen);
// 	assert(0 == iRes);
// 	if (0 != iRes)
// 	{
// 		pDataRepository->writeToRepository(mainCmd, subCode, 0, MSG_VALUE::DS_WRITE_FINISH, (char*)pData, pDataLen);
// 	}
	switch (subCode)
	{
	case SUB_CSG_INIT_HALL:
	{
			log("HALL...Done, %d, %d", nDataLen, sizeof(CMD_HallInfo));
			CMD_HallInfo *pInitHall = (CMD_HallInfo *)pData;
			if (nDataLen != sizeof(CMD_HallInfo))
				return;
			CMD_CS_GetGameDownLoadURL stData;
			stData.iGameID = 0;
			sendData(MAIN_MSG_HALL, SUB_CS_GAME_DOWNLOAD_URL, (char*)(&stData), sizeof(CMD_CS_GetGameDownLoadURL));

			// 获得消息数目
			CMD_UserRecord userRecord;
			userRecord.lUserId = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
			sendData(MAIN_MSG_HALL, SUB_CSG_USER_MSG_RECORD, (char*)&userRecord, sizeof(userRecord));

			// 获取邮件数据
			CMD_Mail userMail;
			userMail.llUid = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
			userMail.iType = 4;
			sendData(MAIN_MSG_USER, SUB_CS_MAIL, (char*)&userMail, sizeof(userMail));

			break;
	}
	case  SUB_SC_GAME_HALL_LIST_SUCC://获取大厅游戏信息 成功
	{
		if (nDataLen != sizeof(CMD_SC_HALLGAME_LIST))
			 return;
		CMD_SC_HALLGAME_LIST * pHALLGAME_LIST = (CMD_SC_HALLGAME_LIST*)pData;

// 		log("获取大厅游戏信息 nGameId = %ld, szAndrDownAddr = %s, szAppDownAddr = %s, szGameName = %s, szPacketName = %s, szVersionNO = %s, szGameIntro = %s,szGameName =%s", "bIsForbidRun = %d",
// 			 pHALLGAME_LIST->nGameId,
// 			 pHALLGAME_LIST->szAndrDownAddr,
// 			 pHALLGAME_LIST->szAppDownAddr,
// 			 pHALLGAME_LIST->szGameName,
// 			 pHALLGAME_LIST->szPacketName,
// 			 pHALLGAME_LIST->szVersionNO,
// 			 pHALLGAME_LIST->szGameIntro,
// 			 pHALLGAME_LIST->szIOSGameName,
// 			 pHALLGAME_LIST->bIsForbidRun);
// 		log("SIX_PlayerMgr::GetInstance()->SetGameInfoStruct-----------------------");
		GNS_PlayerMgr::GetInstance()->SetGameInfoStruct(pHALLGAME_LIST);
		break;
	}
	case SUB_SC_GAME_HALL_LIST_FINISH:
	{
		//GNS_PlayerMgr::GetInstance()->setGetAllGame(true);

		int iRes = 0;
		GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
		assert(pGnsClientFrame);

		IEventBridge::EVENT_INFO eventInfo;
		eventInfo.iMainCode = mainCmd;
		eventInfo.iSubCode = subCode;
		iRes = eventInfo.copyToBody((char*)pData, nDataLen);
		assert(0 == iRes);
		if (0 == iRes)
		{
			IEventBridge::getInstance()->pushBack(eventInfo);
		}
		break;
	}
	case SUB_SC_GAME_DOWNLOAD_URL_SUC://获取游戏下载地址 成功
	{
		if (sizeof(CMD_SC_GetGameDownLoadURLResult) != nDataLen)
		 return;

		CMD_SC_GetGameDownLoadURLResult * pGetGameDownLoadURLResult = (CMD_SC_GetGameDownLoadURLResult*)pData;

		if (pGetGameDownLoadURLResult->iRet == 0)
		{
			if (pGetGameDownLoadURLResult->iGameID == 101) //android
			{
				 GNS_PlayerMgr::GetInstance()->SetTwoCodeInfo(pGetGameDownLoadURLResult->szResText);
			}
			else if (pGetGameDownLoadURLResult->iGameID == 102) //ios
			{
				 GNS_PlayerMgr::GetInstance()->SetTwoCodeInfo(pGetGameDownLoadURLResult->szResText, 1);
			}
		}
		else
		{
		 log("%s", pGetGameDownLoadURLResult->szResText);
		}
		break;
	}
	default:	break;
	}
}

bool GNS_ClientHall::OnHandleUIMsg(const int &msgCmd, const void *msgData)
{
	switch (msgCmd) {
        case LOAD_FINISH:
        {
            
            break;
        }
        default:
            break;
    }
    return true;
}

void GNS_ClientHall::DisConnect()
{
}

void GNS_ClientHall::sendData(const int &mainCmd, const int &subCmd, char *msgData, const int &dataLen)
{
	GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
	assert(pGnsClientFrame);
	pGnsClientFrame->sendMsg(HALLSERVER, mainCmd, subCmd, (char*)msgData, dataLen);
}

void GNS_ClientHall::HandlePacketUser(int mainCmd, int subCode, unsigned char *pData, int pDataLen)
{
	int iRes = 0;

	GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
	assert(pGnsClientFrame);
	
	IEventBridge::EVENT_INFO eventInfo;
	eventInfo.iMainCode = mainCmd;
	eventInfo.iSubCode = subCode;
	iRes = eventInfo.copyToBody((char*)pData, pDataLen);
	//assert(0 == iRes);
	if (0 == iRes)
	{
		IEventBridge::getInstance()->pushBack(eventInfo);
	}

	switch (subCode)
	{
		case SUB_CSG_SEL_USER_INFO: // 查询牌友
		{
			break;
		}
		case SUB_CSG_INVITE_FRIEND: // 好友邀请消息
		{
			CMD_InviteFriendInfo *pInviteFriend = (CMD_InviteFriendInfo *)pData;
			if (!pInviteFriend || sizeof(CMD_InviteFriendInfo) != pDataLen)
				return;

			map<long long, CMD_InviteFriendInfo> *pMap = GNS_PlayerMgr::GetInstance()->getInviteFriend();
			if (pMap->end() != pMap->find(pInviteFriend->lInvitedUserId))
				break;

			pMap->insert(make_pair(pInviteFriend->lInvitedUserId, *pInviteFriend));
			break;
		}
		case SUB_SC_USER_FRIENDS: // 好友列表
		{
			CMD_FriendInfo *pFriendInfo = (CMD_FriendInfo *)pData;
			if (!pFriendInfo || sizeof(CMD_FriendInfo) != pDataLen)
			 return;
				map<long long, CMD_FriendInfo> *pMap = GNS_PlayerMgr::GetInstance()->getFriendList();
				if (pFriendInfo->lFriendUserId > 0)
				{
					pMap->insert(make_pair(pFriendInfo->lFriendUserId, *pFriendInfo));
				}
			break;
		}
		case SUB_SC_NOTICE_MSG: // 广播消息
		{
			CMD_NoticeMsg *pNoticeMsg = (CMD_NoticeMsg *)pData;
			if (!pNoticeMsg || pDataLen != sizeof(CMD_NoticeMsg))
				break;
			break;
		}
		default:
		{
			 break;
		}
	}
}